package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.LoginActivity;
import com.weiyi.mashangzhu.Roomer.Activity.RecommendActivity;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeHavePageBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class HomeThemeAdapter extends PagerAdapter {
    ViewPager viewPager;
    private View mCurrentView;
    private Context context;
    private List<ThemeHavePageBean.DataBean.HouselistBean> list;
    private ImageView themeCollect;
    Boolean[] flags;
    int i = 0;
    private int mChildCount = 0;


    public HomeThemeAdapter(ViewPager viewPager, Context context, List<ThemeHavePageBean.DataBean.HouselistBean> list) {
        this.viewPager = viewPager;
        this.context = context;
        this.list = list;

    }

    //刷新适配器重写的重要方法
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            flags = new Boolean[list.size()];
            for (int i = 0; i < list.size(); i++) {
                flags[i] = false;
            }
            return i = list.size() <= 3 ? list.size() + 1 : 3 + 1;
        } else {
            return 0;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        View view;
        if (position == i - 1) {
            view = View.inflate(context, R.layout.item_more, null);
        } else {
            view = View.inflate(context, R.layout.item_home_theme, null);

            ImageView themeImg = (ImageView) view.findViewById(R.id.theme_img);
            TextView priceNum = (TextView) view.findViewById(R.id.price_num);
            themeCollect = (ImageView) view.findViewById(R.id.theme_collect);
            ImageView themeHead = (ImageView) view.findViewById(R.id.theme_head);
            TextView themeNick = (TextView) view.findViewById(R.id.theme_nick);
            TextView themeContent = (TextView) view.findViewById(R.id.theme_content);
            Glide.with(context).load(API.BaseApi_url + list.get(position).getHImg1()).error(R.mipmap.errer).into(themeImg);
            GlideImgManager.glideLoader(context, API.BaseApi_url + list.get(position).getUHead(), R.mipmap.errer, themeHead, 0);
            priceNum.setText(list.get(position).getHPrice() + "");
            themeContent.setText(list.get(position).getHIntro() + "");
            themeNick.setText(list.get(position).getUNickname() + "");
            if (list.get(position).getIsCollect().equals("0")) {
                themeCollect.setBackgroundResource(R.mipmap.collect);
            } else if (list.get(position).getIsCollect().equals("1")) {
                themeCollect.setBackgroundResource(R.mipmap.collect1);
            }
            themeCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SPUtils.get(context, "isLogin", "0").equals("0")) {
                        UiUtil.startActivity(context, LoginActivity.class);
                    } else if (SPUtils.get(context, "isLogin", "0").equals("1")) {
                        //已登录 进行收藏操作
                        if (list.get(position).getIsCollect().equals("0")) {

                            OkHttpUtils.get().addParams("token", SPUtils.get(context, "token", "") + "")
                                    .addParams("id", list.get(position).getId() + "")
//                                    .addParams("favstatus","0")
                                    .url(API.BaseApi_R + API.CollectHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            T.showShort(context, "收藏失败");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                if (jsonObject.getInt("status") == 0) {
                                                    list.get(position).setIsCollect("1");
                                                    notifyDataSetChanged();
                                                    T.showShort(context, "收藏成功");
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        } else if (list.get(position).getIsCollect().equals("1")) {

                            OkHttpUtils.get().addParams("token", SPUtils.get(context, "token", "") + "")
                                    .addParams("id", list.get(position).getId() + "")
//                                    .addParams("favstatus","1")
                                    .url(API.BaseApi_R + API.CollectHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            T.showShort(context, "取消收藏失败");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                if (jsonObject.getInt("status") == 0) {
                                                    list.get(position).setIsCollect("0");
                                                    notifyDataSetChanged();
                                                    T.showShort(context, "取消收藏成功");
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        }
                    }
                }
            });
            view.setTag(position);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position < list.size()) {
                    SPUtils.put(context, "theme_click_id", position);
                    Intent intent = new Intent(context, HouseDetailsActivity.class);
                    intent.putExtra("id", list.get(position).getId() + "");
                    intent.putExtra("city", list.get(position).getHCity() + "");
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, RecommendActivity.class);
                    intent.putExtra("info", "themehouse");
                    context.startActivity(intent);
                }

            }
        });
        container.addView(view);

        return view;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentView = (View) object;
    }

    public View getPrimaryItem() {
        return mCurrentView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}